/**
 * Created by 荆安喜 on 2017/8/30.
 */
$(function () {
    var ad = appData(),
        pageNumNow = 1,
        paramsT,
        dataPages

    $(function () {
        var parms = {
            account: $('#account').val(),
            accountText: $('#accountText').val()
        }
        paramsT = parms
        //初始化渲染
        //分页
        showTable(true, 1, 'KMGA_ALARM_SEARCH', 'alarmList')
        $('#alarmRetrieval').on('click', alarmRetrieval)
    })


    //alarmRetrieval告警检索按钮
    function alarmRetrieval() {
        var parms = {
            account: $('#account').val(),
            accountText: $('#accountText').val()
        }
        paramsT = parms
        showTable(true, 1, 'KMGA_ALARM_SEARCH', 'alarmList')

    }

    //分页信息
    function showTable(dontRenderPager, page, url, domId) {
        paramsT.page = page
        paramsT.pageSize = 15
        ad.selectList(url, paramsT, function (data) {
            $("#" + domId).empty();
            if (data.content.length === 0) {
                $("#" + domId).append("<tr><td colspan='4'  class='colspanCss'><h1>暂无数据</h1></td></tr>");
                $("#pageList").empty();
                return;
            }

            if (data.content <= appApi("PAGE_SIZE")) {
                $("#pageList").empty();
                return;
            }
            $.get('scripts/app/safe/dataRegulation/alarmSearch.tmpl', function (tmpl) {
                $("#" + domId).empty();
                dataPages = data
                $.tmpl(tmpl, data).appendTo("#" + domId);
                //配置事件
                $('.icon-setting').on('click', deploy)
                //删除事件
                $('.icon-delete').on('click', delAlarm)
                //table 排序插件
                $('table').tablesort().data('tablesort');
                //input效果 批量删除注册
                var bat = new batch('checkAll')
                bat.delete('delAll',batchDel)

                if (dontRenderPager) {
                    renderPager(data.totalRecord);
                }
            })

        });
    }

    function renderPager(totalRecord, nowPage) {
        $("#pageList").pagination(totalRecord, appApi("PAGE_SIZE"), {
            callback: function (page_id, jq) {
                pageNumNow = page_id + 1;
                showTable(false, pageNumNow, 'KMGA_ALARM_SEARCH', 'alarmList');
            }
        }, nowPage);
    }

    //配置页面
    function deploy() {
        $('.dialog-head span').html('配置设备')
        var id = $(this).attr('data-id')
        $(".dialog").show()
    }

    //删除事件
    function delAlarm() {
        var id = $(this).attr('data-id')
        ad.del('KMGA_DEVICE_DEL', {id: id}, function () {
        })
    }

    //批量删除事件
    function batchDel(parms) {
        ad.insert('KMGA_BATCH_DEL',parms,function () {
            showTable(false, pageNumNow, 'KMGA_ALARM_SEARCH', 'alarmList');
            $('#checkAll').iCheck('uncheck')
            _as.jAlert("<p>删除成功</p>")
        })
    }
})